package com.github.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author fatfish
 * @version 1.0
 * @date 2025/8/20 13:57
 */
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 获取前端参数
        String method = req.getParameter("method");
        if ("add".equals(method)) {
            req.getSession().setAttribute("msg", "执行了add方法");
        } else if ("delete".equals(method)) {
            req.getSession().setAttribute("msg", "执行了delete方法");
        } else {
            req.getSession().setAttribute("msg", "执行了其他方法");
        }

        // 2. 调用业务逻辑层（此处省略）

        // 3. 跳转到页面，视图转发
        req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
//         resp.sendRedirect("/WEB-INF/jsp/hello.jsp"); // 重定向方式
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
